import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Login extends JPanel implements ActionListener {

    private JButton loginBtn;
    private Start frame;
    private JTextField zhk;
    private JPasswordField mik;
    private int com=3;
    private int ind;

    public int getInd() {
        return ind;
    }

    public void setInd(int ind) {
        this.ind = ind;
    }

    private Icon anj = Util.getIcon("/images/buttonLand.jpg");

    public Login(Start frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        JLabel titleLabel = new JLabel();
        titleLabel.setBounds(0, 0, 0, 0);
        this.add(titleLabel);

        JLabel zh = new JLabel("账号：");
        zh.setBounds(250, 150, 80, 50);
        zh.setForeground(Color.WHITE);
        zh.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(zh);

        zhk = new JTextField();
        zhk.setBounds(330, 160, 170, 30);
        this.add(zhk);

        JLabel mim = new JLabel("密码：");
        mim.setBounds(250, 200, 80, 50);
        mim.setForeground(Color.WHITE);
        mim.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(mim);

        mik = new JPasswordField();
        mik.setBounds(330, 210, 170, 30);
        this.add(mik);

        loginBtn = new JButton(anj);
        loginBtn.setBounds(350, 320, 100, 50);
        this.add(loginBtn);
        loginBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String a=zhk.getText();
        String m= String.valueOf(mik.getPassword());
        ArrayList<User> users=frame.getUser().getInformation();

        if (e.getSource() == loginBtn) {
            com--;
            for (int i=0;i<3;i++) {
                if (a.equals(users.get(i).getAccount())&&m.equals(users.get(i).getCode())){
                   ind=i;
                    frame.switchPanel("menu");
                    return;
                }
            }
            JOptionPane.showMessageDialog(this,"账号或密码输入错误你还有"+com+"次机会");
        }
        if (com==0){
            System.exit(0);
        }
    }
}
